package cn.cheqi.recovery.api.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * 生成MD5密文工具类
 */

public class MD5Util {

    /**
     * 根据旧的字符串通过MD5加密
     * @param oldStr
     * @return
     */
    public static String createMD5Str(String oldStr){
        try {
            MessageDigest md = MessageDigest.getInstance("md5");
            //通过MD5计算摘要
            byte[] mdStr = md.digest(oldStr.getBytes("UTF-8"));
            String newStr = Base64.getEncoder().encodeToString(mdStr);
            //返回经过BASE64算法加密后的字符串
            return newStr;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }
}
